﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Script.Serialization;

namespace MyCode.Project.Infrastructure.Common
{
    //微信推广支持接口开发
    public class QrCodeApi
    {
        public static string SessionAccessToken = "";//access_token缓存 其他接口的通行证
        JavaScriptSerializer Jss = new JavaScriptSerializer();
        #region 申请带参数的临时/永久二维码
        /// <summary>
        /// 调用微信接口获取带参数临时二维码的ticket
        /// 使用方法：https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET
        /// </summary>
        /// <param name="scene_id">二维码带的参数</param>
        /// <returns>json:ticket:换取二维码的凭证，expire_seconds:凭证有效时间，url:二维码解析后的地址。此处返回ticket 否则返回错误码</returns>
        public string GetQrcode(string appid, string appsecret, Int32 scene_id)
        {
            string QrcodeUrl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={0}";//WxQrcodeAPI接口
            string AccessToken = GetAccessToken(appid, appsecret);//拉取AccessToken
            QrcodeUrl = string.Format(QrcodeUrl, AccessToken);
            string PostJson = "{\"expire_seconds\": 1800, \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": " + scene_id + "}}}";
            string ReText = CommonMethod.WebRequestPostOrGet(QrcodeUrl, PostJson);//post提交
            Dictionary<string, object> reDic = (Dictionary<string, object>)Jss.DeserializeObject(ReText);
            if (reDic.ContainsKey("ticket"))
            {
                return reDic["ticket"].ToString();//成功
            }
            else
            {
                return reDic["errcode"].ToString();//返回错误码
            }
        }

        /// <summary>
        /// 调用微信接口获取带参数永久二维码的ticket
        /// 使用方法：https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET
        /// </summary>
        /// <param name="scene_id">二维码带的参数</param>
        /// <returns>json:ticket:换取二维码的凭证，expire_seconds:凭证有效时间，url:二维码解析后的地址。此处返回ticket 否则返回错误码</returns>
        public string GetQrcode(string appid, string appsecret, string scene_str)
        {
            string QrcodeUrl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={0}";//WxQrcodeAPI接口
            string AccessToken = GetAccessToken(appid, appsecret);//拉取AccessToken
            QrcodeUrl = string.Format(QrcodeUrl, AccessToken);
            string PostJson = "{\"expire_seconds\": 1800, \"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": " + scene_str + "}}}";
            string ReText = CommonMethod.WebRequestPostOrGet(QrcodeUrl, PostJson);//此处省略了 WebRequestPostOrGet即为WebHttpRequest发送Post请求
            Dictionary<string, object> reDic = (Dictionary<string, object>)Jss.DeserializeObject(ReText);
            if (reDic.ContainsKey("ticket"))
            {
                return reDic["ticket"].ToString();//成功
            }
            else
            {
                return reDic["errcode"].ToString();//返回错误码
            }
        }
        #endregion


        #region 获取access_token缓存
        public static string GetTokenSession(string AppID, string AppSecret)
        {
            string TokenSession = "";

            if (System.Web.HttpContext.Current.Session[SessionAccessToken] == null)
            {
                TokenSession = AddTokenSession(AppID, AppSecret);
            }
            else
            {
                TokenSession = System.Web.HttpContext.Current.Session[SessionAccessToken].ToString();
            }

            return TokenSession;
        }
        /// <summary>
        /// 添加AccessToken缓存
        /// </summary>
        /// <param name="AppID"></param>
        /// <param name="AppSecret"></param>
        /// <returns></returns>
        public static string AddTokenSession(string AppID, string AppSecret)
        {
            //获取AccessToken
            string AccessToken = GetAccessToken(AppID, AppSecret);
            HttpContext.Current.Session[SessionAccessToken] = AccessToken;
            HttpContext.Current.Session.Timeout = 7200;
            return AccessToken;
        }

        /// <summary>
        /// 获取AccessToken
        /// </summary>
        /// <param name="AppID"></param>
        /// <param name="AppSecret"></param>
        /// <returns></returns>
        public static string GetAccessToken(string AppID, string AppSecret)
        {
            JavaScriptSerializer Jss = new JavaScriptSerializer();
            string respText = CommonMethod.WebRequestPostOrGet(string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", AppID, AppSecret), "");
            Dictionary<string, object> respDic = (Dictionary<string, object>)Jss.DeserializeObject(respText);
            string accessToken = respDic["access_token"].ToString();
            return accessToken;
        }
        #endregion

    }
}
